/**
 * 应用状态组合式函数
 * 提供便捷的应用状态管理钩子
 */

import { computed } from 'vue'
import { useAppStatusStore } from '@/stores/useAppStatusStore'
import { DEPLOY_STATUS, GENERATE_STATUS } from '@/constants/appStatus'
import { getDeployUrl } from '@/config/env'
import type { DeployStatus, GenerateStatus } from '@/constants/appStatus'

/**
 * 使用应用状态
 */
export function useAppStatus(appId: string | number) {
  const appStatusStore = useAppStatusStore()

  // 获取完整状态信息
  const statusInfo = appStatusStore.getCompleteStatusInfo(appId)

  // 确保状态被初始化的函数
  const ensureStatusInitialized = (app?: API.AppVO) => {
    // 如果没有状态且提供了应用数据，则初始化
    if (!appStatusStore.getAppStatus(appId) && app) {
      appStatusStore.updateStatusFromAppVO(app)
    }
  }

  const deployInfo = appStatusStore.getDeployStatusInfo(appId)
  const generateInfo = appStatusStore.getGenerateStatusInfo(appId)

  // 便捷的状态检查
  const isDeployed = computed(() => deployInfo.value?.isDeployed || false)
  const isDeploying = computed(() => deployInfo.value?.isDeploying || false)
  const canDeploy = computed(() => deployInfo.value?.canDeploy || false)
  const isGenerating = computed(() => generateInfo.value?.isGenerating || false)
  const isGenerateCompleted = computed(() => generateInfo.value?.isCompleted || false)

  // 获取部署URL
  const deployUrl = computed(() => {
    const deployKey = deployInfo.value?.deployKey
    const userId = appStatusStore.getAppStatus(appId)?.userId
    return deployKey ? getDeployUrl(deployKey, userId) : null
  })

  // 更新方法
  const updateFromAppVO = (app: API.AppVO) => {
    appStatusStore.updateStatusFromAppVO(app)
  }

  const updateDeployStatus = (status: DeployStatus, deployKey?: string, deployedTime?: string) => {
    appStatusStore.updateDeployStatus(appId, status, deployKey, deployedTime)
  }

  const updateGenerateStatus = (status: GenerateStatus, taskId?: string) => {
    appStatusStore.updateGenerateStatus(appId, status, taskId)
  }

  return {
    // 状态信息
    statusInfo,
    deployInfo,
    generateInfo,

    // 便捷检查
    isDeployed,
    isDeploying,
    canDeploy,
    isGenerating,
    isGenerateCompleted,

    // URL
    deployUrl,

    // 更新方法
    updateFromAppVO,
    updateDeployStatus,
    updateGenerateStatus,

    // 确保状态初始化
    ensureStatusInitialized,

    // 状态常量
    DEPLOY_STATUS,
    GENERATE_STATUS,
  }
}

/**
 * 使用应用列表状态
 */
export function useAppListStatus() {
  const appStatusStore = useAppStatusStore()

  // 批量更新应用状态
  const updateFromAppList = (appList: API.AppVO[]) => {
    appStatusStore.batchUpdateFromAppList(appList)
  }

  // 获取多个应用的状态信息
  const getMultipleAppStatus = (appIds: (string | number)[]) => {
    return computed(() => {
      return appIds.map(appId => {
        const deployInfo = appStatusStore.getDeployStatusInfo(appId).value
        const generateInfo = appStatusStore.getGenerateStatusInfo(appId).value

        return {
          appId,
          deploy: deployInfo,
          generate: generateInfo,
        }
      })
    })
  }

  return {
    updateFromAppList,
    getMultipleAppStatus,
  }
}

/**
 * 兼容旧代码的部署状态计算函数
 */
export function computeDeployStatus(app: API.AppVO): DeployStatus {
  // 优先使用 deployStatus 字段
  if (typeof app.deployStatus === 'number') {
    return app.deployStatus as DeployStatus
  }
  // 向后兼容：基于 deployedTime 判断
  if (app.deployedTime) {
    return DEPLOY_STATUS.DEPLOYED
  }
  return DEPLOY_STATUS.NOT_DEPLOYED
}

/**
 * 兼容旧代码的生成状态计算函数
 */
export function computeGenerateStatus(app: API.AppVO): GenerateStatus {
  return (app.generateStatus as GenerateStatus) || GENERATE_STATUS.NOT_GENERATED
}
